
import java.io.BufferedReader;
import java.io.DataInputStream;
import java.io.FileInputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 *
 * @author theevilelephant
 */
public class Page {

    private int maxLines = 1000;
    private ArrayList<Line> lines = new ArrayList<Line>();

    public Page(String filePath, int maxChars){
        readInFile(filePath, maxChars);
    }
    
    public Line getLine(int line){
        return lines.get(line);
    }

    public List getSubset(int from, int to){
        return lines.subList(from, to);
    }

    public int getNumLines(){
        return lines.size();
    }

    public void readInFile(String filePath, int maxChars){
        try{
            // Open the file that is the first
            // command line parameter
            FileInputStream fstream = new FileInputStream(filePath);
            // Get the object of DataInputStream
            DataInputStream in = new DataInputStream(fstream);
                BufferedReader br = new BufferedReader(new InputStreamReader(in));
            String strLine;
            int lineCount = 0;
            //Read File Line By Line
            while (((strLine = br.readLine()) != null) && (lineCount < maxLines))   {
                if(strLine.length() > maxChars){

                    for(int a = strLine.length()/maxChars; a > 0; a--){
                        lines.add(new Line(strLine.substring(0, maxChars)));
                        strLine = strLine.substring(maxChars, strLine.length());
                    }
                    //strLine = strLine.substring(0, maxChars);
                }

                lines.add(new Line(strLine));
            }
            //Close the input stream
            in.close();
            }catch (Exception e){//Catch exception if any
              System.err.println("Error: " + e.getMessage());
         }
    }

}
